﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLibary;
using System.Collections;
using ToolUltity;
using EntityLibrary;

namespace Medicare
{
    public partial class FrmBalanceInfoDetail : Form
    {
        #region 自定义变量

        private IMedicare.IMedicare medicare;
        private string medicareNo = string.Empty;
        private readonly string REPORTS_FILE = Application.StartupPath + "\\REPORTS\\五莲医保月报表(明细).rmf";
        private RegisterInfoFacade info = new RegisterInfoFacade();
        private DataSet ds = new DataSet();
        private DataView dv = new DataView();
        private int retint = 0;

        private bool InitDLL = false;
        #endregion

        public FrmBalanceInfoDetail()
        {
            InitializeComponent();
            medicare = IMedicare.IMedicareFactory.Instance.CreateIMedicare();//s
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                txtInhosId.Enabled = true;
                txtMedicareNo.Enabled = false;
                txtName.Enabled = false;
                dtBeginTime.Enabled = false;
                dtEndTime.Enabled = false;
            }
        }

        private void radioButton2_Click(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                txtInhosId.Enabled = false;
                txtMedicareNo.Enabled = false;
                txtName.Enabled = true;
                dtBeginTime.Enabled = false;
                dtEndTime.Enabled = false;
            }
        }

        private void radioButton3_Click(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                txtInhosId.Enabled = false;
                txtMedicareNo.Enabled = true;
                txtName.Enabled = false;
                dtBeginTime.Enabled = false;
                dtEndTime.Enabled = false;
            }
        }

        private void radioButton4_Click(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                txtInhosId.Enabled = false;
                txtMedicareNo.Enabled = false;
                txtName.Enabled = false;
                dtBeginTime.Enabled = true;
                dtEndTime.Enabled = true;
            }
        }

        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            InitData();
            string rowFilter = "FLAG = '2' ";
            if (radioButton1.Checked)
            {
                if (!txtInhosId.Text.ToString().Equals(string.Empty))
                {
                    rowFilter += "AND INHOSID = '" + txtInhosId.Text.ToString().Trim() + "'";
                }

            }
            if (radioButton2.Checked)
            {
                if (!txtName.Text.ToString().Equals(string.Empty))
                {
                    rowFilter += "AND hzxm like '" + txtName.Text.ToString().Trim() + "'";
                }

            }
            if (radioButton3.Checked)
            {
                if (!txtMedicareNo.Text.ToString().Equals(string.Empty))
                {
                    rowFilter += "AND MEDICARENO = '" + txtMedicareNo.Text.ToString().Trim() + "'";
                }

            }
            if (radioButton4.Checked)
            {
                //ds = info.FindbyRegistedPatientInfo(dtBeginTime.DateTime, dtEndTime.DateTime);
                //dv = ds.Tables[0].DefaultView;
                //gridControl1.DataSource = dv;

                rowFilter += "AND TRANDT between '" + dtBeginTime.DateTime + "' AND  '" + dtEndTime.DateTime + "'";
            }
            //MessageBox.Show(rowFilter);
            //dv.RowFilter = rowFilter;

            ds = info.FindbyRegistedPatientInfo(rowFilter);
            dv = ds.Tables[0].DefaultView;
            gridControl1.DataSource = dv;
            
        }

        private void FrmBalanceInfoDetail_Load(object sender, EventArgs e)
        {
            InitData();
        }


        private void InitData()
        {
            try
            {

                ds = info.FindbyRegistedPatientInfo();
                dv = ds.Tables[0].DefaultView;
                dv.RowFilter = "FLAG = '2'";
                gridControl1.DataSource = dv;
            }
            catch
            {

                MessageBox.Show("连接HIS服务器失败！请检查医院网络或联系管理员！");
            }
        }

        #region 关闭窗口按钮事件
      
        /// <summary>
        /// 关闭窗口按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ArrayList PrenoList = new ArrayList();
            medicare.FreeHisDLL(PrenoList);
            this.Close();
        }

        #endregion
       

        #region 召回事件
        /// <summary>
        /// 召回事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCallBack_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int rowhandle = gridView1.FocusedRowHandle;
            //string inhosID=lookUpEditEx1.DisplayValue;//InhosID 从哪张dataview中取？

            if (rowhandle <= -1)
            {
                MessageBox.Show("请选择要进行医保召回的病人！");
                return;
            }




            DataRow row = gridView1.GetDataRow(rowhandle);

            if (!InitDLL)
            {

                ArrayList list = new ArrayList();
                retint = medicare.InitHisDLL(list);
                if (retint != 0)
                {
                    MessageBox.Show("医保接口初始化失败，请检查医保网络！");
                    return;
                }
                InitDLL = true;
            }
            ShowPresonInfo();
            string inhosID = row["INHOSID"].ToString();
            if (!medicareNo.Equals(row["MEDICARENO"].ToString()))
            {
                return;
            }
            ArrayList PrenoList = new ArrayList();
            if (MessageBox.Show("您确定要将该病人进行出院召回吗？") == DialogResult.OK)
            {
                retint = medicare.InHosBalance(-2, 0, "0", inhosID, 0, null, ref PrenoList);
                if (retint != 0)
                {
                    MessageBox.Show("出院召回失败！");
                    return;
                }
                MessageBox.Show("出院召回成功！");
            }
            InitData();

        }


        #endregion

        #region 显示读卡信息

        /// <summary>
        /// 显示读卡信息
        /// </summary>
        public void ShowPresonInfo()
        {
            DataSet paientInfo = new DataSet();
            ArrayList PrenoList = new ArrayList();
            retint = medicare.ReadCard(1, "", ref paientInfo, ref PrenoList);
            if (retint != 0)
            {
                return;
            }

            string[] split = PrenoList[0].ToString().Split('^')[2].Split('|');
            //371100287582|3711004348|371121200608092328|马芮|2|01|20060809000000||||80||||||五莲县高泽镇中心小学|371121|96||61|0|2009|0|||||0|0|0|||0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|||0|||||||||||||371121|
            string birthdaystr = "1900-01-01";
            try
            {
                //19400827
                //Convert.ToDateTime(split[39].Substring(0, 4) + "-" + split[39].Substring(4, 2) + "-" + split[39].Substring(6, 2));
                birthdaystr = split[14].Substring(0, 4) + "-" + split[14].Substring(4, 2) + "-" + split[14].Substring(6, 2);
                //birthdaystr = split[7];                 //生日返回的格式 1900-01-01
            }
            catch (Exception)
            {
                birthdaystr = "1900-01-01";
            }


            FrmPersonInfo frm = new FrmPersonInfo();
            frm.PersonName.Text = split[3];
            frm.Sex.Text = split[4];
            frm.birthday.Text = split[6];
            frm.age.Text = Convert.ToString(System.DateTime.Today.Year - Convert.ToDateTime(birthdaystr).Year);
            frm.MedID.Text = split[0];
            medicareNo = split[0];//医保号
            frm.CardNo.Text = split[9];

            frm.unitno.Text = split[16];
            frm.unitname.Text = split[17];
            #region 增加银行卡读卡获取卡余额

            frm.AccountBalance.Text = split[33].ToString();

            PosChantEnity entity = new PosChantEnity();
            entity.MerchanCode = "03";
            entity.MerchanMoney = "";
            entity.POSFlowNo = DateTime.Now.ToString("MMddss");
            entity.CashMemberNo = "";
            entity.CashRegNo = "";
            entity.OrginTerminalNo = "";
            entity.AuthorizationNo = "";
            entity.OrginTranDT = "";
            entity.TerminalType = "7";
            entity.Track1 = "";
            entity.Track2 = "5226085710076865=49121200352600812761";
            entity.Track3 = "996226095710076865=1561560500050000001015812761214000049120=5710076865=000000000=00000000571000000000000";

            frm.AccountBalance.Text =  medicare.ShowBalance(entity);
            //frm.AccountBalance.Text = split[33];
            #endregion
           
            frm.persontype.Text = split[10];
            frm.InHosTimes.Text = split[47].ToString();
            frm.InHosState.Text = split[34] == "1" ? "住院" : "出院";
            frm.textBox16.Text = split[37];//年度帐户支付累计
            frm.textBox15.Text = split[38];//统筹基金支付
            frm.ShowDialog();
        }

        #endregion

        #region 打印按钮触发
        /// <summary>
        /// 打印按钮触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    PrintReport(ds);
                }
            }

            // gridView1.ex
        }

        #endregion

        #region 打印报表
       
        /// <summary>
        /// 打印报表
        /// </summary>
        /// <param name="ds"></param>
        private void PrintReport(DataSet ds)
        {
            RMReportEngine.RMReport rmReport = new RMReportEngine.RMReport();
            rmReport.Init(this, RMReportEngine.RMReportType.rmrtReport);
            rmReport.AddDataSet(ds.Tables[0], "report");

            //string inhosID = ds.Tables[0].Rows[0]["INHOSID"].ToString();
            //string idCardNo = ds.Tables[0].Rows[0]["IDCARDNO"].ToString();
            //string paientname = ds.Tables[0].Rows[0]["hzxm"].ToString();
            //string medicareNo=ds.Tables[0].Rows[0][]
            //string TotalFee = ds.Tables[0].Rows[0]["PRE0"].ToString();
            //string tczc = ds.Tables[0].Rows[0]["PRE1"].ToString();
            //string dbzc = ds.Tables[0].Rows[0]["PRE2"].ToString();
            //string zhzc = ds.Tables[0].Rows[0]["PRE3"].ToString();
            //string xjzc = ds.Tables[0].Rows[0]["PRE4"].ToString();
            //string gwybzzc = ds.Tables[0].Rows[0]["PRE5"].ToString();
            //string zfje = ds.Tables[0].Rows[0]["PRE6"].ToString();
            //string txfdzf = ds.Tables[0].Rows[0]["PRE7"].ToString();
            //string dbfdzf = ds.Tables[0].Rows[0]["PRE8"].ToString();
            //string yydf = ds.Tables[0].Rows[0]["PRE9"].ToString();
            //string trandt = ds.Tables[0].Rows[0]["TRANDT"].ToString();




            //rmReport.AddVariable("患者姓名", paientname, true);
            //rmReport.AddVariable("住院号", inhosID, true);
            //rmReport.AddVariable("身份证号", idCardNo, true);
            //rmReport.AddVariable("本次医疗费用", TotalFee, true);
            //rmReport.AddVariable("本次统筹支出", tczc, true);
            //rmReport.AddVariable("本次大病支出", dbzc, true);
            //rmReport.AddVariable("本次账户支出", zhzc, true);
            //rmReport.AddVariable("本次现金支出", xjzc, true);
            //rmReport.AddVariable("本次公务员补助支出", gwybzzc, true);
            //rmReport.AddVariable("本次自费金额", zfje, true);
            //rmReport.AddVariable("统筹分段支付", txfdzf, true);
            //rmReport.AddVariable("大病分段支付", dbfdzf, true);
            //rmReport.AddVariable("医院垫付", yydf, true);
            //rmReport.AddVariable("交易日期", trandt, true);
            rmReport.AddVariable("开始日期", dtBeginTime.DateTime.ToString("yyyy-MM-dd"), true);
            rmReport.AddVariable("结束日期", dtEndTime.DateTime.ToString("yyyy-MM-dd"), true);
            rmReport.AddVariable("操作员", ToolUltity.LoginUser.USERNAME, true);
            rmReport.AddVariable("制表日期", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), true);
            rmReport.LoadFromFile(REPORTS_FILE);

            ////打印 or 预览
            //if (rdbPreview.Checked)
            //{
            //    rmReport.ShowReport();
            //}
            //else
            //{

            //}

            rmReport.ShowReport();
            //rmReport.PrintReport();
            rmReport.Dispose();
            rmReport.Destroy();
        }

        #endregion

        #region 导出excel
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DataView vi = gridControl1.DataSource as DataView;

                ExcelService.AddExcel(vi.Table);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }


        #endregion

    }
}
